package com.jack.payment.i18;

import cn.hutool.extra.spring.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class MessageUtils {
    private static Logger log = LoggerFactory.getLogger(MessageUtils.class);

    private final static MessageSource message = SpringUtil.getBean(MessageSource.class);

    public static String message(String code, Object... args){
        StringBuilder lanaguage = new StringBuilder();
        try {
            log.info("客户端使用语言是==>{}, 错误代码是==>{}", LocaleContextHolder.getLocale(), code);
            lanaguage.append(message.getMessage(code, args, LocaleContextHolder.getLocale()));
        } catch (NoSuchMessageException e) {
            //lanaguage.append("不支持的语言类型, 请确认你的语言类型");
            lanaguage.append(message.getMessage(code, args, new Locale("zh_CN")));
        }
        return lanaguage.toString();
    }

    public static String messageTitle(String code, Object... args) {
        StringBuilder lanaguage = new StringBuilder();
        try {
            log.info("标题使用语言是==>{}, 三方内容是==>{}", "zh_cn", code);
            lanaguage.append(message.getMessage(code, args, LocaleContextHolder.getLocale()));
        } catch (NoSuchMessageException e) {
            lanaguage.append(message.getMessage(code, args, new Locale("zh_CN")));
        }
        return lanaguage.toString();
    }

    public static String locale() {
       return String.valueOf(LocaleContextHolder.getLocale());
    }
}
